package com.blogs.component;

import java.util.ArrayList;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blogs.entity.BlogDraftFeed;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.FormatTime;
import com.cnblogs.R;
import com.cnblogs.R.color;

public class BlogDraftAdapter extends BaseAdapter{
	private  ArrayList<BlogDraftFeed> feedList = null;
	private ViewHolder holder;
	private Activity context = null;
	private DBSharedPreferences sp;
	public BlogDraftAdapter(Activity context, ArrayList<BlogDraftFeed> feedList) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.feedList = feedList;
		sp= new DBSharedPreferences(context);
	}
	public int getCount() {
		// TODO Auto-generated method stub
		return feedList.size();
	}
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return feedList.get(position);
	}
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null ) {
			convertView = View.inflate(context, R.layout.draft_adapter_template,null);
			holder=new ViewHolder();
			holder.draft_adapter_title_tv=(TextView)convertView.findViewById(R.id.draft_adapter_title_tv);
			holder.draft_adapter_con_tv=(TextView)convertView.findViewById(R.id.draft_adapter_con_tv);
			holder.draft_adapter_err_text_tv=(TextView)convertView.findViewById(R.id.draft_adapter_err_text_tv);
			holder.draft_adapter_public_time_tv=(TextView)convertView.findViewById(R.id.draft_adapter_public_time_tv);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		if(sp.GetIsDarkState())
		{
			holder.draft_adapter_con_tv.getBackground().setLevel(2);
			holder.draft_adapter_con_tv.setTextColor(context.getResources().getColor(color.con_text_night));
		}
		else
		{
			holder.draft_adapter_con_tv.getBackground().setLevel(1);
			holder.draft_adapter_con_tv.setTextColor(context.getResources().getColor(color.con_text_light));
		}
		String title=feedList.get(position).txbTitle;
		title=title==null?"无标题":title;
		holder.draft_adapter_title_tv.setText(title);
		String EditorBody=feedList.get(position).EditorBody.length()>150?feedList.get(position).EditorBody.substring(0, 150)+"...":feedList.get(position).EditorBody;
		EditorBody=EditorBody.replace("\n", "").replace(" ", "");
		holder.draft_adapter_con_tv.setText(EditorBody);
		holder.draft_adapter_err_text_tv.setText(feedList.get(position).err_text);
		holder.draft_adapter_public_time_tv.setText(FormatTime.format(feedList.get(position).draft_time));
		return convertView;
	}
	class ViewHolder 
	{
		TextView draft_adapter_title_tv;
		TextView draft_adapter_con_tv;
		TextView draft_adapter_err_text_tv;
		TextView draft_adapter_public_time_tv;
	}
}
